package client.service;

import common.Message;
import common.MessageStatus;
import common.User;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class ClientService {
    private User user=new User();
    Socket socket;

    public ClientService(){}

    //有点小犹豫，要不要关流？
    //但socket一定不能关
    public boolean loginVerification(String userName,String password){
        boolean verify=false;
        user.setId(userName);
        user.setPassword(password);

        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);


            ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());

            //发送对象
            objOut.writeObject(user);
            objOut.flush();

            //接收服务器验证消息,验证是否登录成功
            Message message = (Message)objIn.readObject();

            if(message.getType()!=MessageStatus.LOGIN_SUCCEED) {
                if (message.getType() == MessageStatus.USERID_ERROR) {
                    System.out.println(userName + " 不存在，无法登录");

                } else if (message.getType() == MessageStatus.PASSWORD_ERROR) {
                    System.out.println(userName + " 密码错误，无法登录");

                } else if (message.getType() == MessageStatus.IS_ONLINE) {
                    System.out.println(userName + " 已经在线，无法登录");

                }
                socket.close();
                return false;
            }


            //成功登录
            verify=true;

            //创建线程
            System.out.println("正在连接服务端");
            UserThread userThread = new UserThread(socket);
            userThread.start();

            //保存线程
            SocketThreadManager.addUserThread(userName,userThread);


            //不在这里接收离线消息，在Thread中接收就好了
//            if(message.isHave_offline_message()){
//                System.out.println("你的离线信息如下：");
//                for (int i = 0; i < message.getOffline_messages(); ++i) {
//                    Message offline_message=(Message)objIn.readObject();
//
//                    if(offline_message.getType_of_chat()==MessageStatus.TEXT_CHAT){
//                        System.out.println(message);
//                    }else if (offline_message.getType_of_chat()==MessageStatus.FILE_CHAT){
//                        System.out.println("\n"+message.getSend_time()+" to"+message.getGetter()+" "+message.getType()+"\n"+
//                                message.getSender()+": 发送 "+message.getFileName()
//                        );
//
//                        FileOutputStream fileOut = new FileOutputStream(ChatService.storeFolder+"\\"+message.getFileName());
//                        fileOut.write(message.getFileBytes());
//                        fileOut.flush();
//                        fileOut.close();
//                    }
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return verify;
    }

    //请求在线好友列表
    public void requestOnlineFriend(){
        //发送信息
        Message message = new Message();
        message.setType(MessageStatus.GET_ONLINE_FRIEND);
        message.setSender(user.getId());

        //拿到线程对应的socket
        Socket socket = SocketThreadManager.getUserThread(user.getId()).getSocket();
        try {
            ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());

            //发送请求message
            objOut.writeObject(message);
            objOut.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //退出
    public void loginOut(){
        //发送信息
        Message message = new Message();
        message.setType(MessageStatus.NORMAL_EXIT);
        message.setSender(user.getId());

        //拿到线程对应的socket
        Socket socket = SocketThreadManager.getUserThread(user.getId()).getSocket();
        SocketThreadManager.removeUserThread(user.getId());

        try {
            ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
            objOut.writeObject(message);
            objOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
